﻿#include "PlayerController.h"
#include "../../../sdl/audioplayer/YAudioPlayer.h"



bool PlayerController::Open(const char* url, void* winid)
{
	//解封装(如果打开失败就直接暂停)
	if (!demux_.Open(url))
	{
		qDebug() << "PlayerController::Open->解封装打开失败";
		return false;
	}
	//视频解码
	auto vp = demux_.CopyVideoPara();
	if (vp)
	{
		//视频总时长
		this->total_ms_ = vp->total_ms;
		//如果解码器打开失败就直接结束
		if (!video_decode_.Open(vp->para))
		{
			qDebug() << "PlayerController::Open->解码器打开失败";
			return false;
		}
		//video_decode_.set_frame_cache(false);
		//用于过滤音频包
		video_decode_.set_stream_index(demux_.getVideoIndex());
		//缓冲
		video_decode_.set_block_size(100);
		//视频渲染
		if (!view_)
			view_ = IVideoRenderView::Create();
		int videoWidth = demux_.getWidth();
		int videoHeight = demux_.getHeight();
		if (!view_->Init(videoWidth, videoHeight, IVideoRenderView::YUV420P, winid))
		{
			qDebug() << "SDL视频渲染器初始化失败";
			return false;
		}

	}

	//以下是音频初始化部分
	auto ap = demux_.CopyAudioPara();
	if (ap)
	{
		//音频解码
		if (!audio_decode_.Open(ap->para))
		{
			qDebug() << "音频解码器打开失败";
			return false;
		}
		//缓冲
		audio_decode_.set_block_size(100);

		//用于过滤视频数据
		audio_decode_.set_stream_index(demux_.getAudioIndex());

		//frame 缓冲
		audio_decode_.set_frame_cache(true);

		//初始化音频播放
		YAudioPlayer::Create()->Open(*ap);

		//设置时间基数
		//double time_base = 0;
	}
	else
	{
		demux_.set_syn_type(YSYN_VIDEO);//根据视频同步
	}

	//解封装数据传到当前类
	demux_.setNext(this);
	return true;
}

//暂停或者播放
void PlayerController::Pause(bool is_pause)
{
	YThread::Pause(is_pause);
	demux_.Pause(is_pause);
	audio_decode_.Pause(is_pause);
	video_decode_.Pause(is_pause);
	YAudioPlayer::Create()->Pause(is_pause);
}

//设置视频播放位置，毫秒
bool PlayerController::Seek(long long ms)
{
	demux_.Seek(ms);
	audio_decode_.Clear();
	video_decode_.Clear();
	YAudioPlayer::Create()->Clear();
	return true;
}

void PlayerController::Stop()
{
	Exit();
	demux_.Stop();
	audio_decode_.Stop();
	video_decode_.Stop();
	Wait();
	if (view_)
	{
		view_->Close();
		delete view_;
		view_ = nullptr;
	}
	YAudioPlayer::Create()->Close();
}


void PlayerController::Do(AVPacket* pkt)
{
	if (audio_decode_.is_open())
		audio_decode_.Do(pkt);
	if (video_decode_.is_open())
		video_decode_.Do(pkt);
}

void PlayerController::Start()
{
	demux_.Start();
	if (video_decode_.is_open())
		video_decode_.Start();
	if (audio_decode_.is_open())
		audio_decode_.Start();
	YThread::Start();
}

//渲染视频 播放音频
void PlayerController::Update()
{
	//渲染视频
	if (view_)
	{
		auto f = video_decode_.GetFrame();
		if (f)
		{
			view_->DrawFrame(f);
			YFreeFrame(&f);
		}
	}

	////音频播放
	auto au = YAudioPlayer::Create();
	auto f = audio_decode_.GetFrame();
	if (!f)return;
	au->Push(f);
	YFreeFrame(&f);
}

/// <summary>
/// 设置视频播放速度
/// </summary>
/// <param name="s"></param>
void PlayerController::SetSpeed(float s)
{
	YAudioPlayer::Create()->SetSpeed(s);
}

void PlayerController::Working()
{
	long long syn = 0;
	auto audioPlayer = YAudioPlayer::Create();
	auto ap = demux_.CopyAudioPara();
	auto vp = demux_.CopyVideoPara();
	video_decode_.set_time_base(vp->time_base);
	while (!isExit_)
	{
		if (isPause())//如果是暂停状态就不往下同步了（音视频同步）
		{
			MSleep(1);
			continue;
		}
		this->pos_ms_ = video_decode_.cur_ms();
		if (ap)
		{
			syn = YRescale(audioPlayer->cur_pts(), ap->time_base, vp->time_base);
			audio_decode_.set_syn_pts(audioPlayer->cur_pts() + 10000);
			video_decode_.set_syn_pts(syn);
		}
		MSleep(1);

		//获取音频的pts，获取视频的pts
		//if(audio_pts - video_pts > 50ms)
		//{
		//		音频块了，视频要加速追赶，或者丢帧
		//}
		// else if(audio_pts-video_pts < 50ms)
		// {
		//		视频块了 视频要等待
		// }
		//
		/* 1.已全部解封装完成 && 2.解码列表集合<=0 ===直接退出*/
		//video_decode_.Clear
	}
}

PlayerController::PlayerController()
{
}

PlayerController::~PlayerController()
{
}



